今天用spring的mvc做国际化配置,出现了这样的错误
“Cannot change HTTP accept header - use a different locale resolution strategy”。
在网上找了一些资料,根本原因是spring source 做了限制,源码如下
public class AcceptHeaderLocaleResolver implements LocaleResolver {
public Locale resolveLocale(HttpServletRequest request) {
return request.getLocale();
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale resolution strategy");
}
}
请注意上面的类,该类允许继承,所以需要改写setLocale方法,源码示范如下
- localeResolver要用CookieLocaleResolver而不是AcceptHeaderLocaleResolver,若是后者则会抛出异常:
throw new UnsupportedOperati